package com.boot.web.controller;

import java.util.Map;

import com.boot.web.entity.Department;
import com.boot.web.service.DepartmentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.boot.communal.paging.InitPaging;
import com.boot.communal.response.ResponseServer;

/**
 * 部门信息控制器
 * @author 【zhouJie】
 * @date 2020-01-14 17:13:41
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @PostMapping("/save")
    public ResponseServer save(Department department) {
        return ResponseServer.success("添加成功", departmentService.saveDepartment(department));
    }

    @PutMapping("/update")
    public ResponseServer update(Department department) {
        return ResponseServer.success("修改成功", departmentService.updateDepartment(department));
    }

    @DeleteMapping("/delete/{id}")
    public ResponseServer delete(@PathVariable Integer id) {
            departmentService.delete(id);
        return ResponseServer.success("部门信息删除成功");
    }

    @DeleteMapping("/batchDelete/{ids}")
    public ResponseServer batchDelete(@PathVariable Integer[] ids) {
        return ResponseServer.success("批量删除成功", departmentService.batchDeleteDepartment(ids));
    }

    @GetMapping("/getPageList")
    public ResponseServer getPageList(@RequestParam Map<String, Object> params) {
        InitPaging.init(params);
        return ResponseServer.success(new PageInfo(departmentService.getPageList(params)));
    }

    @GetMapping("/getAll")
    public ResponseServer getAll() {
        return ResponseServer.success(departmentService.getAll());
    }

    @GetMapping("/getById/{id}")
    public ResponseServer getById(@PathVariable Integer id) {
        return ResponseServer.success(departmentService.getById(id));
    }

    @GetMapping("/getParent")
    public ResponseServer getParentDepartment() {
        return ResponseServer.success(departmentService.getParentDepartment());
    }

    @GetMapping("/getChildren/{id}")
    public ResponseServer getChildrenDepartment(@PathVariable("id") Integer id) {
        return ResponseServer.success(departmentService.getChildrenDepartment(id));
    }

}
